package com.shennong.controller;

import com.shennong.pojo.*;
import com.shennong.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 符整国
 * @version 1.0
 */
@CrossOrigin(origins = "*") // 允许所有来源（在生产环境中请限制来源）
@RestController
@RequestMapping("/users")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    //登录
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO);
        LoginVO loginVO = userService.login(loginDTO);
        if (loginVO == null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success(loginVO);
    }


    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        log.info("用户注册：{}", user);
        try {
            userService.register(user);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.success();
    }
    /**
     * 根据id查询课程
     * @param id
     * @return
     */
    @GetMapping("/findAllCourse/{id}")
    public Result findAllCourse(@PathVariable Integer id){
        log.info("查询课程,userId为:{}",id);
        List<Course> courseList =userService.findAllCourseById(id);
        return Result.success(courseList);
    }


    /**
     * 查询所有课程
     * @return
     */
    @GetMapping("/course")
    public Result getAllCourse() {
        log.info("查询所有课程");
        List<Course> courseList =userService.findAllCourse();
        return Result.success(courseList);
    }


    /**
     *  选课
     * @param enrollment
     * @return
     */
    @PostMapping("/selectCourse")
    public Result selectCourse(@RequestBody Enrollment enrollment){
        log.info("用户选择课程:{}",enrollment);
        userService.selectCourse(enrollment);
        return Result.success();
    }

    /**
     * 取消选课
     * @param enrollment
     * @return
     */
    @DeleteMapping("/cancelCourse")
    public Result cancelCourse(@RequestBody Enrollment enrollment){
        log.info("用户退课:{}",enrollment);
        userService.cancelCourse(enrollment);
        return Result.success();
    }







    /**
     * 请假
     * @param leave
     * @return
     */
    @PostMapping("/leave")
    public Result leave(@RequestBody Leave leave){
        log.info("用户请假:{}",leave);
        userService.leave(leave);
        return Result.success();
    }

    /**
     * 根据用户id查询请假记录
     * @param id
     * @return
     */
    @GetMapping("/leave/{id}")
    public Result getLeaveByUserId(@PathVariable Integer id){
        log.info("根据用户id查询请假记录:{}",id);
        Leave leave = userService.getLeaveByUserId(id);
        if (leave == null){
            return Result.error("该用户没有请假记录");
        }
        return Result.success(leave);
    }

}
